<?php

class Application_Model_UnionUser_qq extends Application_Model_UnionUser_Base {

    const QQ_APPID = '100308219';
    const QQ_appkey = 'fa90dff2536801fa4a608d9052139a93';
    const QQ_callback = 'http://www.uuch.com/user/qqsynconnect/';
    const QQ_scope = 'get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo';

    protected $_openId;
    
    protected $_access_token;

    public function getUnionName() {
        return 'qq';
    }

    public function getLoginUrl() {
        $state = md5(uniqid(rand(), TRUE));
        $_SESSION['qq_state'] = $state;
        $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="
                . self::QQ_APPID . "&redirect_uri=" . urlencode(self::QQ_callback)
                . "&state=" . $state . "&scope=" . self::QQ_scope;
        return $login_url;
    }

    public function isValid() {
        if ($_SESSION['qq_state'] != $_REQUEST['state']) {
            return FALSE;
        }
        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
                . "client_id=" . self::QQ_APPID . "&redirect_uri=" . urlencode(self::QQ_callback)
                . "&client_secret=" . self::QQ_appkey . "&code=" . $_REQUEST["code"];
        $response = $this->get_url_contents($token_url);  
        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
            $msg = json_decode($response);
            if (isset($msg->error)) {
                return false;
            }
        }
        $params = array();
        parse_str($response, $params);

        $this->_access_token = $params["access_token"];
        return true;
    }

    public function getUserInfo() {
        $qqUser = $this->getQqUserInfo();
        $userInfo = new Application_Model_UnionUser_UserInfo();
        $userInfo->nickname = $qqUser['nickname'];
        $userInfo->userKey = $this->_openId;
        return $userInfo;
    }

    protected function get_openid() {
        $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=". $this->_access_token;

        $str = $this->get_url_contents($graph_url);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
        }

        $user = json_decode($str);
        if (isset($user->error)) {
            return false;
        }

        $this->_openId = $user->openid;
        return true;
    }
    
    protected function getQqUserInfo() {
        $get_user_info = "https://graph.qq.com/user/get_user_info?". "access_token=" . $this->_access_token
                . "&oauth_consumer_key=" . $_SESSION["appid"]. "&openid=" . $this->_openId. "&format=json";
        $info = $this->get_url_contents($get_user_info);
        $arr = json_decode($info, true);
        return $arr;
    }

    public function getUserKey() {
        if(!$this->_openId){
            $this->get_openid();
        }
        return $this->_openId;
    }
}